<?php
/**
 * Created by PhpStorm.
 * User: 快定
 * Date: 2021/8/24
 * Time: 11:41
 */

class CateController extends AppController
{
    public $components = array('publicFunction');
    public $layout = null;

    public function index()
    {
        $limit = 10;
        $where = '';
        $page = $_GET['page'] ? $_GET['page'] : 1;
        $count = $this->Cate->CountAll($where);
        $pageCount = ceil($count / $limit);
        $res = $this->Cate->findAll($where, $limit, $page);
        $this->set(array('list'=> $res , 'page' => $page , 'pageCount' => $pageCount));
    }

    public function add()
    {
        $params = $_POST;
        if (!$params['cate_name']) {
            $this->publicFunction->fail(500, '分类名不能为空!');
        }
        $params['create_time'] = time();
        $params['update_time'] = time();
        $res = $this->Cate->add($params);
        if ($res) {
            $this->publicFunction->success($res);
        } else {
            $this->publicFunction->fail();
        }
    }

    public function del()
    {
        $params = $_POST;
        if (!$params['id']) {
            $this->publicFunction->fail();
        }
        $res = $this->Cate->del($params['id']);
        if ($res) {
            $this->publicFunction->success($res);
        } else {
            $this->publicFunction->fail();
        }
    }

    public function edit()
    {
        $params = $_POST;
        if (!$params['id'] || !$params['cate_name']) {
            $this->publicFunction->fail(500, '参数填写错误！');
        }
        $params['update_time'] = time();
        $res = $this->Cate->add($params);
        if ($res) {
            $this->publicFunction->success($res);
        } else {
            $this->publicFunction->fail();
        }
    }
}